package me.helllp.dake.iam.core.dao.impl;

import me.helllp.dake.core.IdGenerator;
import me.helllp.dake.core.bean.DakeAuthAccount;
import me.helllp.dake.core.utils.PoUtil;
import me.helllp.dake.iam.core.bo.ApplicationBo;
import me.helllp.dake.iam.core.convert.ApplicationConvert;
import me.helllp.dake.iam.core.po.DakeApplication;
import me.helllp.dake.iam.core.mapper.DakeApplicationMapper;
import me.helllp.dake.iam.core.dao.IDakeApplicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dake
 * @since 2022-12-05
 */
@Service
public class DakeApplicationServiceImpl extends ServiceImpl<DakeApplicationMapper, DakeApplication> implements IDakeApplicationService {
    @Autowired
    private ApplicationConvert applicationConvert;

    @Autowired
    private IdGenerator idGenerator;

    @Override
    public ApplicationBo createApplication(ApplicationBo bo, DakeAuthAccount user) {
        Assert.notNull(user, "当前用户不存在");
        Assert.notNull(user.getUserAccount(), "当前用户不存在");
        Assert.notNull(bo, "没有用户信息");

        DakeApplication application = this.applicationConvert.toApplication(bo);
        application.setTenantId(user.getTenantId());

        application.setId(this.idGenerator.nextId());
        PoUtil.setInsertPo(application, user.getUserAccount());
        save(application);
        return this.applicationConvert.toApplicationBo(application);
    }

    @Override
    public Boolean deleteApplication(Long applicationId) {
        return Boolean.valueOf(removeById(applicationId));

    }

    @Override
    public Boolean editApplication(ApplicationBo bo, DakeAuthAccount user) {
        DakeApplication application = this.applicationConvert.toApplication(bo);
        PoUtil.setUpdatePo(application, user.getUserAccount());
        return Boolean.valueOf(updateById(application));
    }
}
